package com.hmy.infrastructure.common.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Map;

/**
 * 根据上下文获取用户认证信息
 */
public class UserOAuth2Details {

    /**
     * 用户id
     */
    public static final String ID = "id";

    /**
     * 用户名
     */
    public static final String NAME = "name";

    /**
     * 手机号
     */
    public static final String PHONE = "phone";

    /**
     * 所属部门id
     */
    public static final String DEPARTMENT_ID = "departmentId";

    /**
     * 所属企业id
     */
    public static final String COMPANY_ID = "companyId";

    private static Map<String, Object> getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Map<String, Object> decodedDetails = (Map) authentication.getPrincipal();
        return decodedDetails;
    }

    /**
     * 获取用户名
     *
     * @return String 返回用户名
     */
    public static String getUsername() {

        return getUser().get(NAME).toString();

    }

    /**
     * 获取部门id
     *
     * @return Long 返回部门id
     */
    public static Long getDepartmentId() {

        Object departmentId = getUser().get(DEPARTMENT_ID);
        return Long.valueOf(departmentId.toString());

    }

    /**
     * 获取企业id
     *
     * @return Long 返回企业id
     */
    public static Long getCompanyId() {

        Object companyId = getUser().get(COMPANY_ID);
        return Long.valueOf(companyId.toString());

    }
}
